package com.earthea.scaffold.common.util;

import cn.hutool.core.date.DatePattern;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateUtils {

    public static String covertUtcToSystem(String datetime) {
        if (StringUtils.isNotBlank(datetime)) {
            // 解析为零时区的LocalDateTime
            LocalDateTime localDateTime = LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
            // 转换为零时区的ZonedDateTime
            ZonedDateTime utcZonedDateTime = localDateTime.atZone(ZoneOffset.UTC);
            // 转换为系统默认时区
            ZonedDateTime systemZonedDateTime = utcZonedDateTime.withZoneSameInstant(ZoneId.systemDefault());
            // 格式化为字符串
            return systemZonedDateTime.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
        }
        return datetime;
    }

    public static String covertSystemToUtc(String datetime) {
        if (StringUtils.isNotBlank(datetime)) {
            // 解析为系统默认时区的LocalDateTime
            LocalDateTime localDateTime = LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
            // 转换为系统默认时区的ZonedDateTime
            ZonedDateTime utcZonedDateTime = localDateTime.atZone(ZoneOffset.systemDefault());
            // 转换为零时区
            ZonedDateTime systemZonedDateTime = utcZonedDateTime.withZoneSameInstant(ZoneOffset.UTC);
            // 格式化为字符串
            return systemZonedDateTime.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
        }
        return datetime;
    }

}
